package com.bjpowernode.mvc.controller;

import cn.hutool.core.bean.BeanUtil;
import com.bjpowernode.db.result.IncomeRecordResult;
import com.bjpowernode.mvc.model.result.CommonResult;
import com.bjpowernode.mvc.model.vo.IncomeRecordResultVO;
import com.bjpowernode.mvc.service.inter.IncomeRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class IncomeRecordController {
    private final IncomeRecordService incomeRecordService;


    /**
     * 查询最近收益记录
     * @param uid
     * @param pageNO
     * @param pageSize
     * @return
     */
    @GetMapping("/incomeRecord/info")
    public CommonResult queryIncomeRecord(
            @RequestHeader("uid") Integer uid,
            @RequestParam Integer pageNO,
            @RequestParam Integer pageSize
    ) {

        //1、验证参数
        pageNO = (pageNO == null || pageNO < 1) ? 1 : pageNO;
        pageSize = (pageSize == null || pageSize < 1 || pageSize > 100) ? 6 : pageSize;

        //2、查询最近收益记录
        int offset = (pageNO - 1) * pageSize;
        List<IncomeRecordResult> incomeRecordResultList = incomeRecordService.queryIncomeRecordByUid(uid, offset, pageSize);

        //3、转VO
        List<IncomeRecordResultVO> incomeRecordResultVOList = BeanUtil.copyToList(incomeRecordResultList, IncomeRecordResultVO.class);


        return CommonResult.ok(incomeRecordResultVOList);
    }

}
